﻿using System.IO;

namespace System
{
    public static class StringExtensions
    {
        public static string Indent(this string text, int oldTab, int newTab)
        {
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            using (var writer = new StringWriter())
            using (var reader = new StringReader(text))
            {
                var line = reader.ReadLine();
                do
                {
                    int emptyCount = 0;
                    foreach (var lineChar in line)
                    {
                        if (lineChar == ' ')
                            emptyCount++;
                        else
                            break;
                    }

                    if (emptyCount == 0)
                        writer.WriteLine(line);
                    else
                    {
                        int tabCount = (emptyCount / oldTab);
                        int totalTabs = (tabCount * newTab);
                        var tabString = new string(' ', totalTabs);
                        writer.WriteLine(tabString + line.TrimStart());
                    }

                    line = reader.ReadLine();
                } while (line != null);
                return writer.ToString();
            }
        }
    }
}
